<?php namespace Admin\Model;
use Hdphp\Model\Model;
//商品模型
class Goods extends Model{
//	指定表名
	protected $table = 'goods';
//	自动验证
	protected $validate = array(
	array('gname','required','商品名称必须填写',3,3),
	array('category_cid','required','请选择商品分类',3,3),
	array('brand_bid','required','请选择商品分类',3,3),
	array('artnum','required','货号不能为空',3,3),
	array('unit','required','单位不能为空',3,3),
	array('market_price','required','市场价不能为空',3,3),
	array('shop_price','required','商城价格不能为空',3,3),
	array('inventory','required','库存不能为空',3,3),
	array('click','required','点击次数不能为空',3,3),
	array('click','num:0,65535','点击次数必须为数字',3,3),
	);
//	自动完成
	protected $auto = array(
	array('time','time','function',3,1),
	array('lists_pic','image','method',3,3),
	);
//	添加方法
	public function store($goodsarr){
//		sp($goodsarr);
//		触发自动验证，
		if(!$this->create()) return false;
		$gid = $this->add($goodsarr);
		return $gid;
	}
//	删除方法
	public function del($gid){
//		1、删除商品表
		$this->delete($gid);
//		2、删除商品属性表
		$goodModel = new \Admin\Model\GoodsAttr;
		$goodModel->where("goods_gid={$gid}")->delete();
//		3、删除商品详细
		$goodDeModel = new \Admin\Model\GoodsDetail;
		$goodDeModel->where("goods_gid={$gid}")->delete();
		return true;
	}
//	编辑方法
	public function edit($gid){
		$goodsarr = array(
			'gname' =>$_POST['gname'],
			'nick_name' =>$_POST['nick_name'],
			'des' =>$_POST['des'],
			'unit' => $_POST['unit'],
			'artnum'=>$_POST['artnum'],
			'market_price' => $_POST['market_price'],
			'shop_price' =>$_POST['shop_price'],
			'inventory' =>$_POST['inventory'],
			'click' =>$_POST['click'],
			'brand_bid' =>$_POST['brand_bid'],
			'category_cid' =>$_POST['category_cid'],
			'type_tid' =>$_POST['type_tid'],
			'user_admin_uid' => $_POST['user_admin_uid']
		);
		if($_POST['lists_pic']){
			$goodsarr['lists_pic']=$_POST['lists_pic'];
		}
		if(!$this->create()) return false;
//		验证成功 执行修改
		$this->where("gid={$gid}")->save($goodsarr);
		return true;
	}
}




 ?>